﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Admx_Generator
{
    public class Revision : IEquatable<Revision>
    {
        private uint _major = 1;
        private uint _minor = 0;
        private string p;

        /// <summary>
        /// Create a new instance of Revision
        /// </summary>
        /// <param name="major">An uint between 0...9999</param>
        /// <param name="minor">An uint between 0...99999</param>
        public Revision(uint major, uint minor)
        {
            this._major = System.Math.Min(9999, major);
            this._minor = System.Math.Min(99999, minor);
        }

        /// <summary>
        /// Create a new instance from a string formatted as "major,minor".
        /// </summary>
        /// <param name="revision">A formatted string to create this instance from.</param>
        public Revision(string revision)
        {
            uint major = 1;
            uint minor = 0;
            int index = revision.IndexOf('.');

            uint.TryParse(revision.Substring(0, index), out major);
            uint.TryParse(revision.Substring(index + 1), out minor);
            this.Major = major;
            this.Minor = minor;
        }

        #region {properties}

        /// <summary>
        /// Gets or Sets the MajorVersion of this revision.
        /// </summary>
        public uint Major
        {
            get { return this._major; }
            private set { this._major = value; }
        }

        /// <summary>
        /// Gets or Sets the MinorVersion of this revision.
        /// </summary>
        public uint Minor
        {
            get { return this._minor; }
            private set { this._minor = value; }
        }

        #endregion {properties}

        public override string ToString()
        {
            return String.Concat(this.Major, ".", this.Minor);
        }

        #region (interface)

        public bool Equals(Revision other)
        {
            if (other == null)
                return false;
            return (string.Compare(this.ToString(), other.ToString(), StringComparison.InvariantCultureIgnoreCase) == 0);
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as Revision);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        #endregion (interface)
    }
}
